<?php

/**
 * @Author: chenjianghui
 * @Date:   2018-09-04 15:15:14
 * @Last Modified by:   chenjianghui
 * @Last Modified time: 2018-09-12 09:08:43
 */
namespace app\admin\controller\shop;

use app\common\controller\Backend;
use think\Db;

/**
 * 商家店铺管理
 *
 * @icon fa fa-circle-o
 */
class Shop extends Backend
{
    /**
     * Shop模型对象
     * @var \app\common\model\Shop
     */
    protected $model = null;

    /**
     * 快速搜索时执行查找的字段
     */
    protected $searchFields = 'shop_id';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Shop;

    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            //
            $field = "*";
            $total = $this->model->field($field)->where($where)->count();
            $list  = $this->model->field($field)->where($where)->limit($offset, $limit)->order("create_time DESC")->select();
            //dump($this->model->getLastSql());
            $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
            foreach ($list as $key => $value) {
                if ($value['status'] == 0) {
                    $list[$key]['status'] = '待审核';
                } else if ($value['status'] == 1) {
                    $list[$key]['status'] = '审核通过';
                } else {
                    $list[$key]['status'] = '审核失败';
                }
                $value['fullurl']   = $this->view->config['upload']['cdnurl'] . $value['shop_pic'];
                $area = explode(',', $value['area_id']);
                $area = array_pop($area);
                $value['area_id']   = Db::name('area')->where('id', $area)->value('mergename');
                $value['shop_type'] = $value['shop_type'] == 0 ? '不限' : ($value['shop_type'] == 1 ? '天猫' : '淘宝');
            }
            unset($value);
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $params                  = $this->request->post('row/a');
            $province                = $this->request->post('province');
            $city                    = $this->request->post('city');
            $area                    = $this->request->post('area');
            $params['create_time'] = time();
            $params['update_time'] = time();
            $params['area_id']       = $province . ',' . $city . ',' . $area;
            $params['status']        = 0;
            $params['shop_admin_id'] = session('admin')['id'];
            //验证
            $shopValidate = \think\Loader::validate('Shop');
            try {
                $result = $this->model->validate('Shop.add')->save($params);
                if ($result === false) {
                    $this->error($this->model->getError());
                }
                if ($result) {
                    return json(['code' => 1, 'msg' => '店铺申请成功']);
                } else {
                    return json(['code' => 0, 'msg' => '店铺申请失败']);
                }
            } catch (Exception $e) {
                return json(['code' => 0, 'msg' => '店铺申请失败']);
            }
        }
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $has_task = Db::name('shop_task')->where('shop_id', $ids)->find();
            if($has_task){
                if($has_task['total_task_num'] != $has_task['left_task_num']){
                    $this->error();
                }
            }
            try {
                $result = $this->model->where('shop_id', $ids)->delete();
                if($result){
                    $this->success();
                }else{
                    $this->error();
                }
            } catch (Exception $e) {
                $this->error();
            }
        }
        $this->error();
    }
}
